<div class="ajaxLoading">
<img src='<?php echo $engineReturn['admin_template']?>css/images/loading.gif' width="50" align="middle" />
</div>
<div id="containerInsertForm" style="display:none">
<h2></h2> 
<a href='#' class='aNav' id='view_data_id'>View Sub Data</a>
<?php
//echo "<pre>";
//print_r($engineReturn['record']);
	if(isset($engineReturn['record']['msg']) && is_array($engineReturn['record']['msg']))
		foreach($engineReturn['record']['msg'] as $msg)
		{
			echo "<div class='inlineMessage'><a href='#".$msg[1]."'>".$msg[0]."</a></div>";
		}
?>
<form id="frmEngineInsert" name="frmEngineInsert" method="post" action="#" enctype="multipart/form-data">
<table class="table" width="100%">
<?php
//echo "<pre>";
//print_r($engineReturn);
if(isset($engineReturn['msg']))
foreach($engineReturn['msg'] as $msg)
{
	echo "<div class='inlineMessage'><a href='#".$msg[1]."'>".$msg[0]."</a></div>";
}
foreach($engineReturn['record']['fields'] as $key=>$row)
{
	$value	=	$row['post_value'];
//	echo "<pre>";
//	print_r($value);
//	echo "<br>";
	echo "<tr>";
		echo "<td  width='25%'>".$row['text']."</td>";
		echo "<td>";
		switch($row['input'])
		{
			case "text":
					echo "<input type='text' id='".$row['name']."' name='".$row['name']."' class='rejectChar engineInput ".(($row['attr']['maxlength']==10)?"datepicker":"")."' size='".$row['attr']['size']."' maxlength='".$row['attr']['maxlength']."' title='Input Text' value='".$value."'>";
			break;
			case "text_area":
				echo "<textarea id='".$row['name']."' name='".$row['name']."' class='rejectChar engineInput' cols='".$row['attr']['cols']."' rows='".$row['attr']['rows']."' title='Input Text'>".$value."</textarea>";
			break;
			case "radio":
				if(is_array($value))
					$value = $value[0];
				echo "<table>";
				foreach($row['real_source'] as $rs)
				{
					echo "<tr><td>";
						echo "<input type='radio' id='".$row['name']."' name='".$row['name']."' value='".$rs[0]."' ".(($value==$rs[0])?"checked='checked'":"")."> ".(isset($rs[1])?$rs[1]:$rs[0]);
					echo "</td></tr>";
				}
				echo "</table>";
			break;
			case "select":
				echo "<select id='".$row['name']."' name='".$row['name']."'>";
				echo "<option value=''>-".$row['text']."-</option>";
				foreach($row['real_source'] as $rs)
				{
						echo "<option value='".$rs[0]."' ".(($value==$rs[0])?"selected='selected'":"")."> ".(isset($rs[1])?$rs[1]:$rs[0])."</option>";
				}
				echo "</select>";
			break;
			case "simple_table":
				echo "<select id='".$row['name']."' name='".$row['name']."'>";
				echo "<option value=''>-".$row['text']."-</option>";
				foreach($row['real_source'] as $rs)
				{
						echo "<option value='".$rs['idRec']."'".(($value==$rs['idRec'])?"selected='selected'":"")."> ".$rs['dataRec']."</option>";
				}
				echo "</select>";
			break;
			case "medium_table":
					$valueRec	= "";
					if(is_array($value))
					{
						$valueId	= $value[0];
						$valueRec	= $value[1];
					}
					else
					{
						$valueId	= "";
					}
					echo "<input type='hidden' id='".$row['name']."' name='".$row['name']."' value='".$valueId."'>";
					echo "<div  id='container-".$row['name']."' name='container-".$row['name']."' class='engineInputContainer'>";
						echo "<div id='edit-".$row['name']."' name='edit-".$row['name']."' class='defContainer' title='Press here to switch beetwen Search and View Mode'>";
							echo "<a href='#' class='engineInputEdit' id='edit-button-".$row['name']."' name='edit-button-".$row['name']."'>Edit</a>";
						echo "</div>";

						echo "<div id='search-".$row['name']."' name='search-".$row['name']."' class='defContainer' style='display:none;'>";
							echo "<input type='text' id='search-input-".$row['name']."' name='search-input-".$row['name']."' class='rejectChar engineInput' size='' maxlength=''>";
							echo "<div id='ajax-".$row['name']."' name='ajax-".$row['name']."' class='containerAjax'></div>";
						echo "</div>";

						echo "<div  id='view-".$row['name']."' name='view-".$row['name']."' class='engineInputView'>";
							if(is_array($valueRec) && ($valueId))
							{
								echo "<table class='table'>";
									echo "<tr>";
									foreach($valueRec[1] as $v)
									{
										echo "<th>$v</th>";
									}
									echo "</tr>";
									if(isset($valueRec[0][0]))
									{
										echo "<tr>";
										foreach($valueRec[0][0] as $v)
										{
											echo "<td>$v</td>";
										}
										echo "</tr>";
									}
								echo "</table>";
							}
						echo "</div>";
					echo "</div>";
					echo "<script type='text/javascript'>";
					echo "$(document).ready(function(){";
					echo "$('#edit-button-".$row['name']."').loadMediumTable({
					module_name 		: '".$row['name']."'
					, search_div_id 	: 'search-".$row['name']."'
					, search_input_id 	: 'search-input-".$row['name']."'
					, view_div_id		: 'view-".$row['name']."'
					, ajax_div_id		: 'ajax-".$row['name']."'
					, url				: '".$baseUrlSave.$otority."/engine/".$engineReturn['modulename']."/ajax/medium_table/".$row['name']."/'});";
					echo "});";
					echo "</script>";
			break;
			case "advance_table":
					$valueRec	= "";
//					echo "<pre>";
//					print_r($value);
					if(is_array($value))
					{
						$valueId	= $value[0];
						$valueRec	= $value[1];
					}
					else
					{
						$valueId	= "";
					}
					echo "<input type='hidden' id='".$row['name']."' name='".$row['name']."' value='".$valueId."'>";
					echo "<div  id='container-".$row['name']."' name='container-".$row['name']."' class='engineInputContainer'>";
						echo "<div id='edit-".$row['name']."' name='edit-".$row['name']."' class='defContainer'>";
							echo "<a href='#' class='engineInputEdit' id='edit-button-".$row['name']."' name='edit-button-".$row['name']."' title='Press here to switch beetwen Search and View Mode'>Edit</a>";
						echo "</div>";

						echo "<div id='search-".$row['name']."' name='search-".$row['name']."' class='defContainer' style='display:none;'>";
							echo "<input type='text' id='search-input-".$row['name']."' name='search-input-".$row['name']."' class='rejectChar engineInput' size='' maxlength=''>";
							if(is_array($row['real_source']))
							{
								echo "<div id='filter-".$row['name']."' name='filter-".$row['name']."' class='containerFilter'>";
								echo "<h3>Filter</h3>";
								echo "<table class='table'>";
								foreach($row['real_source'] as $k=>$r)
								{
									echo "<tr>";
									echo "<td><b>".$r['text']."</b></td>";
									echo "<td>";
									switch($r['input'])
									{
										case "text":
											echo "<input type='text' id='filter-id-".$k."' name='filter-id-".$k."' class='engineInput' size='30' maxlength='100'>";
										break;
										case "radio":
											echo "<table>";
											foreach($r['source'] as $rs)
											{
												echo "<tr><td>";
													echo "<input type='radio' id='filter-id-".$k."' name='filter-id-".$k."' value='".$rs[0]."'> ".(isset($rs[1])?$rs[1]:$rs[0]);
												echo "</td></tr>";
											}
											echo "</table>";
										break;
										case "select":
											echo "<select id='filter-id-".$k."' name='filter-id-".$k."'>";
											echo "<option value=''>-".$r['text']."-</option>";
											foreach($r['source'] as $rs)
											{
													echo "<option value='".$rs[0]."'> ".(isset($rs[1])?$rs[1]:$rs[0])."</option>";
											}
											echo "</select>";
										break;
										case "simple_table":
											echo "<select id='filter-id-".$k."' name='filter-id-".$k."'>";
											echo "<option value=''>-".$row['text']."-</option>";
											foreach($r['source'] as $rs)
											{
													echo "<option value='".$rs['idRec']."'> ".$rs['dataRec']."</option>";
											}
											echo "</select>";
										break;
									}
									echo "</td>";
								}
								echo "</table>";
								echo "</div>";
							}
							echo "<div id='ajax-".$row['name']."' name='ajax-".$row['name']."' class='containerAjax'></div>";
						echo "</div>";

						echo "<div  id='view-".$row['name']."' name='view-".$row['name']."' class='engineInputView'>";
							if(is_array($valueRec) && ($valueId))
							{
								echo "<table class='table'>";
									echo "<tr>";
									foreach($valueRec[1] as $v)
									{
										echo "<th>$v</th>";
									}
									echo "</tr>";
									if(isset($valueRec[0][0]))
									{
										echo "<tr>";
										foreach($valueRec[0][0] as $v)
										{
											echo "<td>$v</td>";
										}
										echo "</tr>";
									}
								echo "</table>";
							}
						echo "</div>";
					echo "</div>";
					echo "<script type='text/javascript'>";
					echo "$(document).ready(function(){";
					echo "$('#edit-button-".$row['name']."').loadAdvanceTable({
					module_name 		: '".$row['name']."'
					, search_div_id 	: 'search-".$row['name']."'
					, search_input_id 	: 'search-input-".$row['name']."'
					, view_div_id		: 'view-".$row['name']."'
					, ajax_div_id		: 'ajax-".$row['name']."'
					, filter_div_id 	: 'filter-".$row['name']."'
					, filter_count		: '".count($row['real_source'])."'
					, url				: '".$baseUrlSave.$otority."/engine/".$engineReturn['modulename']."/ajax/advance_table/".$row['name']."/'});";
					echo "});";
					echo "</script>";
			break;
		}
		if(is_array($row['note']) && (count($row['note']>0)))
			foreach($row['note'] as $n)
				echo "<div>$n</div>";
		echo "</td>";
	echo "</tr>";
}

?>
	<tr>
    	<td colspan="2">
        	<input type="hidden" id="init" name="init" value="save">
        	<input type="submit" value="Save" class="button" id="submitInsert">

        </td>
    </tr>
</table>
</form>
</div>
<script type="text/javascript">
	$('#containerInsertForm').delay(100).show('slow', function(){
		$('.ajaxLoading').slideUp('slow');
	});
	$('#view_data_id').navigateToView({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/sub/data/nav/<?php echo $engineReturn['record']['idMaster'];?>/',
		div_form 		: 'containerInsertForm',
		div_loading 	: 'ajaxLoading',
	});
	$('#frmEngineInsert').submitInsertSubData({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/sub/data/ins/<?php echo $engineReturn['record']['idMaster'];?>/',
		postValue		: {
			<?php
			foreach($engineReturn['record']['fields'] as $key=>$row)
			echo $key." : {0:'".$row['name']."', 1:'".$engineReturn['record']['fields'][$key]['input']."'}, ";
			?>
			},
		len				: <?php echo count($engineReturn['record']['fields']);?>,
		div_container	: 'containerGrid',
		div_grid		: 'containerInsertForm',
		div_loading 	: 'ajaxLoading',
	});
//	$('.rejectChar').rejectChar();
</script>